package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.List;
import java.util.Set;


@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;


    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user == null){
            return null;
        }

        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        if(roles != null && roles.size() >0){
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions= permissionDao.findByRoleId(roleId);
                if(permissions != null && permissions.size() >0){
                    role.setPermissions(permissions);
                }

            }
            user.setRoles(roles);
        }
        return user;
    }

    @Override
    public void add(User user, Integer[] roleIds) {
        //新增用户,操作t_user表
        userDao.add(user);
        //设置检查组和检查项的多对多的关联关系,操作t_user_role表
        Integer userId = user.getId();
        this.setuserAndrile(userId,roleIds);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = userDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List<Integer> findroleIdsByuserId(Integer id) {
        return userDao.findroleIdsByuserId(id);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        //修改检查组基本信息,操作检查组t_checkgroup表
        userDao.edit(user);
        //清理当前检查组关联的检查项,操作中间关系表
        userDao.deleteAssocication(user.getId());
        //重新建立当前检查组和检查项的关联关系
        Integer userId = user.getId();
        this.setuserAndrile(userId,roleIds);
    }

    @Override
    public void deleteById(Integer id) {
        userDao.deleteAssocication(id);
        userDao.deleteById(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return userDao.findAll();
    }

    public void setuserAndrile(Integer userId,Integer[] roleIds){
        if(roleIds != null && roleIds.length > 0){
            for(Integer roleId : roleIds){
                Map<String,Integer> map = new HashMap<>();
                map.put("userId",userId);
                map.put("roleId",roleId);
                userDao.userAndrole(map);
            }
        }
    }


    //根据UserName查询用户角色ID
    @Override
    public Integer findRoleByUserName(String username) {
        return roleDao.findRoleByUserName(username);
    }
    //根据用户角色ID查询菜单
    @Override
    public List<Menu> getMenuByRoleId(Integer roleId) {
        List<Menu> menuByRoleId = menuDao.getMenuByRoleId(roleId);
        for (Menu menu : menuByRoleId) {
            List<Menu> menuByRoleId1 = menuDao.getMenuByRoleId2(menu.getId(),roleId);
            menu.setChildren(menuByRoleId1);
        }
        return menuByRoleId;
    }
    //test
}
